<!-- TMS订单管理 - 综合表单页面 -->
<template>
  <div class="page-container" v-loading="loading">

    <div class="p_fill flex_center" v-if="dataError">
      {{ $tt("tip_no_select_order_place_retry_into_for_list") }}
    </div>
    <el-form v-else ref="form" class="flex_col flex_cell" :model="form" label-position="right">

      <ex_header_spiler :title="$tt('label_base_info')"></ex_header_spiler>

      <div class="flex_row_wrap">
        <el-form-item :label="$tt('label_logistics')" class="form_item_mini">
          {{ form.logisticsProductsName }}
        </el-form-item>
        <el-form-item :label="$tt('label_origin')" class="form_item_mini">
          {{ form.startLineName }}
        </el-form-item>
        <el-form-item :label="$tt('label_destination')" class="form_item_mini">
          {{ form.endLineName }}
        </el-form-item>
        <el-form-item :label="$tt('label_tms_receipt_scheduling')" class="form_item_mini">
          <el-switch v-model="isFocusDispatch">
          </el-switch>
        </el-form-item>
        <el-form-item :label="$tt('label_operate')" class="form_item_mini">
          <el-button size="medium" type="primary" @click="submit">{{ $tt('label_create_plan') }}</el-button>
        </el-form-item>

      </div>
      <el-form-item :label="$tt('label_tms_receipt_shipping_order')">
        {{ $tt('label_tms_order_sum') }}{{ form.trackOrderInfoParamList.length }}{{ $tt('label_tms_order_count') }}
        <el-button class="mg_l2" size="mini" @click="orderDetailClick">
          {{ $tt('label_fms_subject_detail') }}</el-button>
      </el-form-item>

      <ex_header_spiler :title="$tt('label_tms_line_operation_project')">
        <el-button size="medium" type="primary" @click="clickNewScheme">{{ $tt('新建运营方案') }}</el-button>
      </ex_header_spiler>
      <el-radio-group v-model="schemeId" class="w_fill flex_cell flex_col" style="min-height: 150px;">
        <ex_super_table v-bind="schemeOption" :data="form.planSchemeDTOList" ref="TableScheme" :autoFlex="true"
          @row-click="(row, column, event) => schemeId = row.id">
          <template v-slot:radio="{ row }">
            <el-radio style="margin-left: 10px;" :label="row.id" :key="row.id">{{ "" }}</el-radio>
          </template>
          <template v-slot:expand="{ row }">
            <el-steps align-center :active="0" finish-status="success">
              <el-step :title="item.placeName" icon="el-icon-truck" :key="index"
                v-for="(item, index) in row.schemeLineInfoDTOList"
                :description="$enumShow(item.type, 'LogisticsProductsTypeEnum')"></el-step>
            </el-steps>
          </template>
        </ex_super_table>
      </el-radio-group>
    </el-form>
  </div>
</template>
<script>
import superTablePageFunction from '@/mixin/super-table-page-function';


import emitOnFunction from '@/mixin/emit-on-function';
let emitKey = 'tms>track_create_plan'
import router_params from '@/mixin/router_params';

let default_obj = {
  planSchemeDTOList: [],
  trackOrderInfoParamList: [],
}
export default {
  title: "生成运输计划",
  mixins: [superTablePageFunction,emitOnFunction(emitKey), router_params],
  components: {},
  data() {
    return {
      schemeId: null,
      isFocusDispatch: true,
      trackIds: [],
      dataError: false,
      loading: false,
      form: _.cloneDeep(default_obj),
      routeId: null,
      orderOption: {
        model: [
          {
            key: 'trackNumbers', label: 'label_tms_track_transport_order_no',
            'min-width': 140, 'show-overflow-tooltip': true,
          },
          {
            key: 'orderCodes', label: 'label_tms_track_associated_order_number',
            'min-width': 140, 'show-overflow-tooltip': true,
          },
          {
            key: 'customerNames', label: 'label_customer_name',
            'min-width': 140, 'show-overflow-tooltip': true,
          },
          {
            key: 'trackTaskType', label: 'label_fms_subject_order_type',
            'min-width': 140, 'show-overflow-tooltip': true,
            formatter: (r, c, v, i) => this.$enumShow(v, "TrackTaskTypeEnum")
          },
          {
            key: 'addedServices', label: 'label_tms_track_added_services',
            'min-width': 140, 'show-overflow-tooltip': true,
          },
        ]
      },
      schemeOption: {
        model: [

          { slot: 'radio', 'width': 120, },
          { type: 'expand', slot: 'expand', },
          {
            key: 'schemeName', label: 'label_tms_line_operation_project_name',
            'min-width': 140, 'show-overflow-tooltip': true,
          },
          {
            key: 'routeName', label: 'label_tms_track_route',
            'min-width': 140, 'show-overflow-tooltip': true,
          },
          {
            key: 'channelName', label: 'label_tms_track_involved_channel',
            'width': 140, 'show-overflow-tooltip': true,
          },
          {
            key: 'prescription', label: 'label_tms_track_prescription_time',
            'width': 140, 'show-overflow-tooltip': true,
          },
          {
            key: 'measuringCosts', label: 'label_tms_track_estimated_cost',
            'width': 140, 'show-overflow-tooltip': true,
          },
          {
            key: 'remark', label: 'label_remark',
            'width': 140, 'show-overflow-tooltip': true,
          },
          {
            type: 'operate',
            'width': 140, operate: [
              { label: '复制新增', click: (row) => this.$router.push({ path: '/page/tms/line/scheme_operate', query: { id: row.id, routeId: this.routeId, emitKey} }) },
            ]
          },
        ],
      },
    };
  },
  computed: {
    orderTips() {
      return this.form.trackOrderInfoParamList.map(it => it.trackNumbers).join(',')
    }
  },
  methods: {
    clickNewScheme() {
      this.$router.push({ path: '/page/tms/line/scheme_operate', query: { routeId: this.routeId,emitKey } })
    },
    orderDetailClick() {

      this.$msgbox_table({
        note: this.$tt('单据详情'),
        tableProp: {
          data: this.form.trackOrderInfoParamList,
          model: this.orderOption.model
        },
      }, { customClass: 'dialog_default' })
    },
    init_data_default(into_params) {
      let selection = into_params.selection
      let trackIds = []
      let list = _.split(selection, ',');

      list.forEach(it => {
        trackIds.push(it)
      })
      if (_.isEmpty(trackIds)) {
        this.dataError = false
        return this.$message.error(this.$tt('tip_no_select_order_place_retry_into_for_list'))
      } else {
        this.trackIds = trackIds
        this.reload()
      }
    },
    reload() {
      this.loading = true
      this.dataError = false

      this.$ajax({
        url: "/tms-api/task/TmsOrderInfoRpc/generatePlan.json",
        method: 'post', type: "json",
        data: {
          trackIds:this.trackIds
        },
        complete: () => {
          this.loading = false
        },
        success: (res) => {
          this.trackIds = this.trackIds
          res.data.planSchemeDTOList.forEach(it => it.check = true)
          let first = _.head(res.data.trackOrderInfoParamList)
          let firstPlan = _.head(res.data.planSchemeDTOList)
          if (firstPlan) this.schemeId = firstPlan.id
          this.form = Object.assign({}, _.cloneDeep(default_obj), {
            planSchemeDTOList: res.data.planSchemeDTOList,
            trackOrderInfoParamList: res.data.trackOrderInfoParamList,

            logisticsProductsName: first ? first.logisticsProductsName : "",
            startLineName: res.data.startLineName,

            endLineName: res.data.endLineName,
          })
          // this.form = Object.assign({}, _.cloneDeep(default_obj), res.data)

          this.routeId = _.head(this.form.trackOrderInfoParamList).routeId
          // this.fixTabLabel()
        }
      })
    },
    submit: _.throttle(function (item) {
      this.checkForm()
    }, 700),
    checkForm() {
      if (!this.schemeId) {
        return this.$message.error(this.$tt('请选择运营方案'))
      }
      let scheme = _.find(this.form.planSchemeDTOList, it => it.id == this.schemeId)

      this.$msgbox_table({
        note: [
          "集中拣货 : " + this.$enumShow(this.isFocusDispatch ? "Y" : "N", "YesOrNoEnum"),
          "运营方案 : " + scheme.schemeName,
        ],
        tableProp: {
          data: this.form.trackOrderInfoParamList,
          model: [
            { key: 'trackNumbers', label: '运输订单编号', 'min-width': 120, 'show-overflow-tooltip': true, },
            { key: 'orderCodes', label: '订单平台编号', 'min-width': 120, 'show-overflow-tooltip': true, }
          ],
        },
      }, { type: 'warning', customClass: 'dialog_small', title: this.$tt("是否创建计划"), })
        .then(() => {
          this.submitNet()
        }).catch(() => {
        });

    },
    submitNet() {

      this.$ajax({
        url: '/tms-api/task/TmsOrderInfoRpc/createPlan.json',
        method: 'post', type: 'json',
        loading: true,
        data: {
          trackIds: this.trackIds,
          schemeId: this.schemeId,
          isFocusDispatch: this.isFocusDispatch ? "Y" : "N",
        },
        success: (responseData) => {
          this.$message.success(this.$tt('tip_submit_success'))

          this.return_emit(null, "reload")
          this.$tag.tagClose(null, "/page/tms/order/track_manage", true)
        },
        complete: () => {
        }
      });
    },

  }
};

</script>
<style></style>
